from datetime import datetime

from django.db import models


class SubscriptionMessageManager(models.Manager):
    def get_queryset(self):
        return super(SubscriptionMessageManager, self).get_queryset().filter(
            subscription__is_enabled=True, user__is_enabled=True)


class SubscriptionMessage(models.Model):
    """
    公众号发给用户的消息

    """
    subscription = models.ForeignKey('Subscription', models.CASCADE, 'user_messages')
    user = models.ForeignKey('User', models.CASCADE, 'subscription_messages')
    create_time = models.DateTimeField(default=datetime.now, db_index=True)

    content = models.TextField(default='', max_length=256, db_index=True)
    
    enabled = SubscriptionMessageManager()

    class Meta:
        db_table = 'subscription_message'
        ordering = ['-create_time']
